﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Xml
Imports System.Reflection


Partial Class 测段


    Public 标志 As String
    Public 起测点 As String
    Public 前尺号码 As String
    Public 后尺号码 As String
    Public 数据加密方法 As String
    Public 加密校验 As String
    Public 记录程序 As String
    Public 记录器编号 As String
    Public 记录器型号 As String
    Public 起测点经度 As String
    Public 起测点纬度 As String
    Public 加密数据 As String
    Public 测段使用 As String
    Public 测段备注 As String
    Public 文件名 As String
    Public 检测高差 As String
    Public 检测备注 As String


    Public Property 测站s As IList(Of 测站) = New List(Of 测站)()
    Public Property 项目s As IList(Of 项目) = New List(Of 项目)()
    Public Property 观测事件s As IList(Of 观测事件) = New List(Of 观测事件)()
    Public Property 测段小结s As IList(Of 测段小结) = New List(Of 测段小结)()

    Public Property 水准路线 As 水准路线


    Public Property routeNO As String ' 测段编号


    ''' <summary>
    ''' 路线号+测段号
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getRouteNo() As String

        Return routeNO
    End Function



    ' (of summary)
    ' 解析测段XML数据
    ' (of /summary)
    ' (of param name="xxNode")(of /param)
    ' (of returns)(of /returns)
    Public Shared Function Paras(xxNode As XmlNode) As 测段

        Dim tmpClass As 测段 = New 测段()
        Dim nameList As XmlAttributeCollection = xxNode.Attributes
        For Each xxattribute As XmlAttribute In nameList
            Try

                Dim tmpType As Type = tmpClass.GetType()
                Dim tmpP As FieldInfo = tmpType.GetField(xxattribute.Name)
                tmpP.SetValue(tmpClass, xxattribute.Value)
            Catch ex As Exception

            End Try

        Next


        Dim childList As XmlNodeList = xxNode.ChildNodes '取得row下的子节点集合
        For Each xxxNode As XmlNode In childList

            If (xxxNode.Name.Equals("测站")) Then

                Dim tmpClass1 As 测站 = 测站.Paras(xxxNode)
                tmpClass1.测段 = tmpClass
                tmpClass.测站s.Add(tmpClass1)
            End If
            If (xxxNode.Name.Equals("项目")) Then

                Dim tmpClass1 As 项目 = 项目.Paras(xxxNode)
                tmpClass1.测段 = tmpClass
                tmpClass.项目s.Add(tmpClass1)
            End If
            If (xxxNode.Name.Equals("观测事件")) Then

                Dim tmpClass1 As 观测事件 = 观测事件.Paras(xxxNode)
                tmpClass1.测段 = tmpClass
                tmpClass.观测事件s.Add(tmpClass1)
            End If
            If (xxxNode.Name.Equals("测段小结")) Then

                Dim tmpClass1 As 测段小结 = 测段小结.Paras(xxxNode)
                tmpClass1.测段 = tmpClass
                tmpClass.测段小结s.Add(tmpClass1)
            End If
        Next


        Return tmpClass

    End Function


    ''' <summary>
    ''' 获取上午观测站数
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getAM_ZZS() As String
        Dim startEvent As 观测事件 = Nothing, endEvent As 观测事件 = Nothing
        Dim cnt As Integer = 0
        For Each tmp As 观测事件 In 观测事件s
            If "BK0,GO0,C1 ,C2 ,RB1,RG1".Contains(tmp.标记) Then
                '开始计数
                startEvent = tmp
            End If
            If "EN ,ST ".Contains(tmp.标记) Then
                '结束事件
                If IsNothing(startEvent) Then
                Else
                    endEvent = tmp
                End If
            End If


            If (IsNothing(startEvent) Or IsNothing(endEvent)) Then
            Else
                If Val(endEvent.时间.Substring(8, 2)) <= 12 Then
                    '上午时间
                    cnt = cnt + (Val(endEvent.测站) - Val(startEvent.测站))
                End If
            End If
        Next
        Return cnt
    End Function

    ''' <summary>
    ''' 获取下午观测站数
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getPM_ZZS() As String
        Dim startEvent As 观测事件 = Nothing, endEvent As 观测事件 = Nothing
        Dim cnt As Integer = 0
        For Each tmp As 观测事件 In 观测事件s
            If "BK0,GO0,C1 ,C2 ,RB1,RG1".Contains(tmp.标记) Then
                '开始计数
                startEvent = tmp
            End If
            If "EN ,ST ".Contains(tmp.标记) Then
                '结束事件
                If IsNothing(startEvent) Then
                Else
                    endEvent = tmp
                End If
            End If


            If (IsNothing(startEvent) Or IsNothing(endEvent)) Then
            Else
                If Val(endEvent.时间.Substring(8, 2)) > 12 Then
                    '上午时间
                    cnt = cnt + (Val(endEvent.测站) - Val(startEvent.测站))
                End If
            End If
        Next
        Return cnt
    End Function

    ''' <summary>
    ''' 获取总站数
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getZZS() As String

        Dim a As Object = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then

            Return "0"

        Else

            Return a.测站

        End If
    End Function
    ''' <summary>
    ''' 等级
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getDJ() As String

        Dim a = (From p In 项目s Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        ElseIf (a.等级.Equals("国标一等")) Then
            Return "DNT1"
        ElseIf (a.等级.Equals("国标二等")) Then
            Return "DNT2"
        ElseIf (a.等级.Equals("国标三等")) Then

            Return "DNT3"
        ElseIf (a.等级.Equals("国标四等")) Then
            Return "DNT4"
        Else
            Return "DNT0"
        End If

    End Function

    ''' <summary>
    ''' 起测点
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getStartName() As String
        Return Me.起测点

    End Function
    ''' <summary>
    ''' 结束点
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getEndName() As String


        Dim a = (From p In 测段小结s Select p).LastOrDefault()
        If IsNothing(a) Then

            Return "0"

        Else

            Return a.结束点
        End If

    End Function

    'ElevationG=往测_高差       从(of 测段小结 结束点="Ⅰ大榆25-2基上" 结束点经度="112.4837" 结束点纬度="39.314" 视距累差="0.920000" 高差="-18.46655"获取
    'distanceG=往测_距离        从(of 测段小结 结束点="Ⅰ大榆25-2基上" 结束点经度="112.4837" 结束点纬度="39.314" 视距累差="0.920000" 高差="-18.46655" 距离="3.09km"获取
    'ElevationB=返测_高差       从(of 测段小结 结束点="山阴(1189)下" 结束点经度="112.48361" 结束点纬度="39.32369" 视距累差="-2.060000" 高差="18.46274"获取
    'distanceB=返测_距离        从(of 测段小结 结束点="山阴(1189)下" 结束点经度="112.48361" 结束点纬度="39.32369" 视距累差="-2.060000" 高差="18.46274" 距离="3.07km"获取


    ' (of summary)
    ' 仪器_编号
    ' (of /summary)
    ' (of returns)(of /returns)
    Public Function getApparatusNO() As String
        Dim a As 项目 = (From p In 项目s Select p).FirstOrDefault()
        If IsNothing(a) Then

            Return ""

        Else
            Dim b = (From p In a.仪器s Select p).FirstOrDefault()
            If (IsNothing(b)) Then

                Return ""

            Else
                Return b.编号
            End If
        End If

    End Function
    ''' <summary>
    ''' 往测_高差
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getElevationG() As String
        If (Me.标志 = "GO0" Or Me.标志 = "RG1") Then
            Dim a = (From p In 测段小结s Select p).LastOrDefault()
            If IsNothing(a) Then
                Return ""
            Else
                Return a.高差
            End If
        Else
            Return "0"
        End If
    End Function
    ''' <summary>
    ''' 往测_距离
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getDistanceG() As String

        If (Me.标志 = "GO0" Or Me.标志 = "RG1") Then

            Dim a = (From p In 测段小结s Select p).LastOrDefault()
            If IsNothing(a) Then
                Return ""
            Else
                Return a.距离.Substring(0, a.距离.Length - 2)
            End If
        Else
            Return "0"
        End If
    End Function

    ''' <summary>
    ''' 往测_前尺号码
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getStaffNOG() As String


        If (Me.标志 = "GO0" Or Me.标志 = "RG1") Then


            Return Me.前尺号码

        Else


            Return "0"
        End If



    End Function
    ''' <summary>
    ''' 返测_前尺号码
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getStaffNOB() As String

        If (Me.标志 = "BK0" Or Me.标志 = "RB1") Then


            Return Me.前尺号码

        Else


            Return "0"
        End If
    End Function


    ''' <summary>
    ''' 返测_高差
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getElevationB() As String

        If (Me.标志 = "BK0" Or Me.标志 = "RB1") Then

            Dim a = (From p In 测段小结s Select p).LastOrDefault()
            If IsNothing(a) Then
                Return ""
            Else

                Return a.高差
            End If
        Else
            Return "0"
        End If
    End Function
    ''' <summary>
    ''' 返测_距离
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function getDistanceB() As String

        If (Me.标志 = "BK0" Or Me.标志 = "RB1") Then

            Dim a = (From p In 测段小结s Select p).LastOrDefault()
            If IsNothing(a) Then
                Return ""
            Else
                Return a.距离.Substring(0, a.距离.Length - 2)
            End If
        Else
            Return "0"
        End If
    End Function
    ''' <summary>
    ''' 开始时间
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getStartDate() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return a.时间
        End If
    End Function
    ''' <summary>
    ''' 结束时间
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetEndDate() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return a.时间
        End If
    End Function
    ''' <summary>
    ''' 观测时间
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getDateUD() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "UD " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return a.时间
        End If
    End Function
    ''' <summary>
    ''' 上下标志高差
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getElevationUD() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "UD " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Dim b As 数据 = (From p In a.数据s Select p).FirstOrDefault()
            If IsNothing(b) Then
                Return ""
            Else
                Return b.高差
            End If
        End If
    End Function
    ''' <summary>
    ''' 上下标志点名
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getNameUD() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "UD " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            If Val(a.测站 = 0) Then
                Dim b As 数据 = (From p In a.数据s Select p).FirstOrDefault()
                If IsNothing(b) Then
                    Return ""
                Else
                    Return b.点名
                End If
            Else
                Dim b As 数据 = (From p In a.数据s Select p).FirstOrDefault()
                If IsNothing(b) Then
                    Return ""
                Else
                    Return b.点名
                End If

            End If
        End If
    End Function
    ''' <summary>
    ''' 开始温度
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getStartTemperature() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return a.温度
        End If
    End Function
    ''' <summary>
    ''' 结束温度
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getEndTemperature() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return a.温度
        End If
    End Function
    ''' <summary>
    ''' 观测事件数量
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getBTH() As String

        Return Me.观测事件s.Count

    End Function
    ''' <summary>
    ''' 开始点呈像
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_CX1() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.呈像)
        End If

    End Function
    ''' <summary>
    ''' 结束点呈像
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_CX2() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.呈像)
        End If

    End Function
    ''' <summary>
    ''' 开始点呈像
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_TY1() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.太阳方向)
        End If

    End Function
    ''' <summary>
    ''' 结束点太阳方向
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_TY2() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.太阳方向)
        End If

    End Function


    ''' <summary>
    ''' 开始点风向
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_FX1() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.风向)
        End If

    End Function
    ''' <summary>
    ''' 结束点风向
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_FX2() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.风向)
        End If

    End Function

    ''' <summary>
    ''' 开始点风速
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_FS1() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.风速)
        End If

    End Function
    ''' <summary>
    ''' 结束点风速
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_FS2() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.风速)
        End If

    End Function

    ''' <summary>
    ''' 开始点道路
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_DL1() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.道路)
        End If

    End Function
    ''' <summary>
    ''' 结束点道路
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_DL2() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.道路)
        End If

    End Function
    ''' <summary>
    ''' 开始点土质
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_TZ1() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.土质)
        End If

    End Function
    ''' <summary>
    ''' 结束点土质
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_TZ2() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_XX(a.土质)
        End If

    End Function

    ''' <summary>
    ''' 天气和云量
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_TQ1() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = Me.标志 Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_TQ(a.天气, a.云量)
        End If
    End Function
    ''' <summary>
    ''' 天气和云量
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_TQ2() As String
        Dim a As 观测事件 = (From p In 观测事件s Where p.标记 = "EN " Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return getCD_TQ(a.天气, a.云量)
        End If
    End Function
    ''' <summary>
    ''' 天气解码
    ''' </summary>
    ''' <param name="a"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_TQ(a As String, b As String) As String
        Dim code As String = ""
        code = getCD_XX(a) & getCD_XX(b)
        Return code
    End Function
    ''' <summary>
    ''' 编码类统一解码
    ''' </summary>
    ''' <param name="a"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getCD_XX(a As String) As String
        If IsNothing(a) Then
            Return ""
        Else
            Return a.Substring(0, 1)
        End If
    End Function

    Function Get结束点经度() As String
        Dim a As 测段小结 = (From p In 测段小结s Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return a.结束点经度
        End If
    End Function

    Function 结束点纬度() As String
        Dim a As 测段小结 = (From p In 测段小结s Select p).LastOrDefault()
        If IsNothing(a) Then
            Return ""
        Else
            Return a.结束点纬度
        End If
    End Function

End Class
